def month_before(a_time)
    a_time - 28 * 24 * 60 * 60
end

def header(date)
    date.strftime("Changes since %Y-%m-%d:")
end

def subsystem_line(subsystem_name, count_change)
    if count_change == 0
        "#{subsystem_name.rjust(14)} - (#{count_change})"
    elsif 0 < count_change && count_change <= 2
        "#{subsystem_name.rjust(14)} * (#{count_change})"
    else
	"#{subsystem_name.rjust(14)} #{stars(count_change)} (#{count_change})"
    end
end

def stars(number_of_changes)
    "*"*((number_of_changes/5.0).round)
end

def number_of_changes(subfolder_name, start_date)
    extract_number_of_changes(get_svn_log(subfolder_name, start_date))
end

def extract_number_of_changes(text)
    lines = text.split("\n")
    dashed_lines = lines.find_all {|line| line.include?("----")}
    dashed_lines.length-1
end

def get_svn_log(subsystem, start_date)
    timespan = "--revision 'HEAD:{#{start_date}}'"
    path = "svn://rubyforge.org/var/svn/churn-demo"

    `svn log #{timespan} #{path}/#{subsystem}`
end

def get_count_from_line(subsystem_line)
    /\((\d+)\)/.match(subsystem_line)[1].to_i
end

def order_subsystem_line(subsystem_line)
    subsystem_line.sort do |one, another|
	number_one = get_count_from_line(one)
	number_another = get_count_from_line(another)
	-( number_one <=> number_another)
    end
end

if $0 == __FILE__
#start_date = month_before(Time.now)

start_date = Time.local(2004,07,30)

puts header(start_date)

system_names = ['audit', 'fulfillment','persistence', 'ui', 'util', 'inventory']

lines = system_names.collect do |subsystem|
    subsystem_line(subsystem, number_of_changes(subsystem, start_date))
end

puts order_subsystem_line(lines)
end
